<script setup>
import Cookies from "js-cookie";
import {User, Lock} from "@element-plus/icons-vue";
import {ref} from "vue";
import {login} from "@/api/user.js";
import { ElMessage, ElMessageBox } from 'element-plus'
import router from "@/router/index.js";

import {userStore} from '@/stores/user.js'
const store = userStore();

const form = ref(null);
const loading = ref(false);
const loginForm = ref({
  username: "",
  password: "",
  rememberMe: false
});

//点击登录按钮
const submitForm = (formEl) => {
  if (!formEl) return
  loading.value = true;
  // console.log("点击登录按钮")
  // 勾选了需要记住密码设置在 cookie 中设置记住用户名和密码
  if (loginForm.value.rememberMe) {
    Cookies.set("username", loginForm.value.username, { expires: 30 });
    Cookies.set("password", loginForm.value.password, { expires: 30 });
    Cookies.set("rememberMe", loginForm.value.rememberMe, { expires: 30 });
  } else {
    // 否则移除
    Cookies.remove("username");
    Cookies.remove("password");
    Cookies.remove("rememberMe");
  }

  formEl.validate(async (valid) => {
    if (valid) {
      const res = await login(loginForm.value.username, loginForm.value.password);
      // console.log(res);
      if (res.code === 40000) {
        // 登录成功
        ElMessage.success("登录成功");
        // 获取用户信息
        const userInfo = await store.getUserInfo()
        // console.log(userInfo)
        // 获取用户角色
        const userRole = userInfo.user_type
        // 根据角色跳转不同主页面
        router.push(`/${userRole}/home`)
      }else {
        await ElMessageBox.alert('请检查用户名或密码', '登录失败', {
          confirmButtonText: '好的'
        })
        console.log("登录失败");
      }
    } else {
      console.log('表单错误')
    }
    loading.value = false;
  })

};

//校验表单
const rules = ref({
  username: [
    {required: true, message: "请输入用户名", trigger: "blur"},
    {min: 5, max: 18, message: "长度在 5 到 18 个字符", trigger: "blur"},
  ],
  password: [
    {required: true, message: "请输入密码", trigger: "blur"},
    {min: 5, max: 18, message: "长度在 8 到 18 个字符", trigger: "blur"},
  ],
});

// 获取cookie
function getCookie() {
  const username = Cookies.get("username");
  const password = Cookies.get("password");
  const rememberMe = Cookies.get("rememberMe");
  loginForm.value = {
    username: username === undefined ? loginForm.value.username : username,
    password: password === undefined ? loginForm.value.password : password,
    rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
  };
}

getCookie();
</script>

<template>
  <div class="login-page">
    <el-form
      class="login-form"
      ref="form"
      :model="loginForm"
      :rules="rules"
      size="large"
      autocomplete="off"
    >
      <el-form-item>
        <h1>订单管理系统</h1>
      </el-form-item>
      <el-form-item class="form-item" :prop="'username'">
        <el-input
          v-model="loginForm.username"
          class="ipt1"
          :prefix-icon="User"
          placeholder="请输入用户名"
        ></el-input>
      </el-form-item>
      <el-form-item class="form-item" :prop="'password'">
        <el-input
          class="ipt2"
          v-model="loginForm.password"
          :prefix-icon="Lock"
          type="password"
          placeholder="请输入密码"
        ></el-input>
      </el-form-item>
      <el-form-item class="form-item item-password">
        <el-checkbox v-model="loginForm.rememberMe" style="height: 40px;">记住密码</el-checkbox>
        <router-link class="forget-password" :to="'/forgetPassword'" href="#">找回密码</router-link>
      </el-form-item>
      <el-form-item class="form-item">
        <el-button class="button" type="primary" @click="submitForm(form)" :disabled="loading">
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2024-2025 sy.vip All Rights Reserved.</span>
    </div>
  </div>
</template>

<style scoped lang="scss">
.login-page {
  display: flex;
  flex-direction: column;
  justify-content: center;
  height: 100vh;
  background-image: url("@/assets/images/background.jpg");
  background-size: cover;

  .login-form {
    display: flex;
    width: 400px;
    flex-direction: column;
    align-items: center;
    height: 360px;
    margin: auto auto;
    background-color: rgba(255, 255, 255, 0.7);
    border-radius: 10px;

    .form-item {
      width: 80%;

      .button {
        width: 100%;
        margin: 0 auto;
      }
    }

  }

  .forget-password {
    height: 41px;
    margin-left: 176px;
    color: #539dfe;
    text-decoration: none;
    &:hover {
      color: #82bafe;
      cursor: pointer;

    }
  }

  .el-login-footer {
    height: 40px;
    line-height: 40px;
    position: fixed;
    bottom: 0;
    width: 100%;
    text-align: center;
    color: #fff;
    font-family: Arial,serif;
    font-size: 12px;
    letter-spacing: 1px;
  }
}
</style>
